package org.example.dao;

import org.example.DBConnection;
import org.example.entity.Product;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaomengzi
 * @create 2025-08-04 下午2:52
 */
public class ProductDAO {
    Connection connection;
    public ProductDAO() throws SQLException {
        this.connection = DBConnection.getConnection();
    }
    //创建商品
    public int createProduct(Product product) throws SQLException {
        String sql = "insert into products (name,description,price,stock) values(?,?,?,?)";
        try(PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, product.getName());
            ps.setString(2, product.getDescription());
            ps.setBigDecimal(3, product.getPrice());
            ps.setInt(4, product.getStock());
            ps.executeUpdate();
            try (ResultSet rs = ps.getGeneratedKeys()) {
                if (rs.next()) {
                    return rs.getInt(1);
                }
            }
        }
        return -1;
    }
    //通过id获取商品
    public Product getProduct(int id) throws SQLException {
        String sql = "select * from products where id=?";
        try(PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, id);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    return new Product(
                            rs.getInt("id"),
                            rs.getString("name"),
                            rs.getString("description"),
                            rs.getBigDecimal("price"),
                            rs.getInt("stock"),
                            rs.getTimestamp("created_at").toLocalDateTime(),
                            rs.getTimestamp("updated_at").toLocalDateTime()
                    );
                }
            }
        }
        return null;
    }
    //获取所有商品
    public List<Product> getAllProducts() throws SQLException {
        List<Product> products = new ArrayList<>();
        String sql = "select * from products";
        try(PreparedStatement ps = connection.prepareStatement(sql)) {
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    products.add(new Product(
                            rs.getInt("id"),
                            rs.getString("name"),
                            rs.getString("description"),
                            rs.getBigDecimal("price"),
                            rs.getInt("stock"),
                            rs.getTimestamp("created_at").toLocalDateTime(),
                            rs.getTimestamp("updated_at").toLocalDateTime()
                    ));
                }
            }
        }
        return products;
    }
    //更新库存
    public boolean updateStock(int id, int stock) throws SQLException {
        String sql = "update products set stock=stock - ? where id=?";
        try(PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, stock);
            ps.setInt(2, id);
            if (ps.executeUpdate() > 0){
                return true;
            }else{
                return false;
            }
        }
    }
}
